<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

$aVar = array();
$aDefDati = array();
$msg = aVarCalcTabDati(reqVarInt('id'), 0, $aVar, $aDefDati, 2);
$dataRif = $aVar['B_DATA_RIF'];
$idSetDati = $aVar['B_ID_SET_DATI'];

$dataRifPrec = '';
$aVarBP = null;
$aDefDatiP = array();
if (reqVarInt('idp')>0) {
	$aVarBP = array();
	$msgBp = aVarCalcTabDati(reqVarInt('idp'), 0, $aVarBP, $aDefDatiP, 2);
	$dataRifPrec = $aVarBP['B_DATA_RIF'];
}
$sCmdL = '';
$sCmdL .= "<a class='cmd1' href='bilRicPdf.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
		."&idt=".reqVarInt('id_def_tdati_bilric')."&xsn=".XSN."'>PDF</a>";

$sCmdR = '';
$sCmdR .= "<a class='cmd1' href='bilCeeVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
	."&xsn=".XSN."'>bilancio</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
if (reqVarInt('idp')>0) {
	$sCmdR .= "<a class='cmd1' href='renFinVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
		."&xsn=".XSN."'>rendiconto finanziario</a>";
	$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
	$sCmdR .= "<a class='cmd1' href='indiciVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
		."&xsn=".XSN."'>indici di bilancio</a>";
}
$sCmdR .= cmdTorna('torna');

$aOpzTesta['aMenuVar']['bilancio CEE']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/bilCeeVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['bilancio riclassificato']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/bilRicVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['rendiconto finanziario']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/renFinVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['indici']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/indiciVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');

testaHtml('', $msgPag, 'ratg',$aOpzTesta);
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('idp');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" align="center" colspan='20'><b>Azienda</b></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.fisc.</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_FISC']?></td>
      <td class="lab1" align="left" colspan='3'>Cod CCIAA</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_CCIAA']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Ragione sociale</td>
      <td class="txt1" align="left" colspan='17'>
      	<a class='txt1' href='anagVis.php?id=<?=$aVar['B_ID_AZIENDA']?>&xsn=<?=XSN?>'>
      	<?=$aVar['B_RAG_SOC']?></a></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Sede</td>
      <td class="txt1" align="left" colspan='17'><?=$aVar['B_SEDE']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.sett.</td>
      <td class="txt1" align="left" colspan='3'><?=$aVar['B_COD_SETT']?></td>
      <td class="txt1" align="left" colspan='14'><?=$aVar['B_DESCR_SETT']?></td>
   </tr>
	<tr>
		<td class='tit1'align='center' colspan='20'><b>BILANCIO RICLASSIFICATO</b></td>
	<tr>
<?
if (trim($aVar['B_NOTA'])!='') {
?>
	<tr>
		<td class='txt1'align='left' colspan='20'>
			<?=getHtmlViewNote(trim($aVar['B_NOTA']));?>
		</td>
	<tr>
<?
}
$aGeDati = getGerarcDefDati($aDefDati,2);

foreach($aGeDati as $codX => $aX) {
	echoDato($codX,$aX,$aVar,$aVarBP,0);
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td> 
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();


function echoDato($codX, $aX, &$aVar, &$aVarBP, $liv) {
	global $dataRif, $dataRifPrec, $objSess, $objDB;
	$aCSez = array(''=>'-- non sezione --');
	$aCSez['spatt'] = 'Stato patrimoniale - Attivo';
	$aCSez['sppas'] = 'Stato patrimoniale - Passivo';
	$aCSez['cecon'] = 'Conto economico';
	$aCSez['altro'] = 'Altri dati';
	$aCSez['calco'] = 'Dati calcolati';
	
	if ($aX['attr']['cod_sez']!='') {
		echo "<tr><td class='txt1'align='center' colspan='12'><b>".$aCSez[$aX['attr']['cod_sez']]."</b></td>\n"
			."</td><td class='txt1' align='center' colspan='3'><i>".YMD2DMY($dataRif)."</i></td>\n"
			."</td><td class='txt1' align='center' colspan='3'><i>".YMD2DMY($dataRifPrec)."</i></td>\n"
			."</td><td class='txt1' align='center' colspan='2'><i>".(($dataRifPrec=='')?'':'Variaz.%')."</i></td>\n<tr>\n";
	} else {
		$codV = 'Q_'.$codX;
		$numX = '';
		$valX = '';
		switch ($aX['attr']['tipo_valore']) {
			case 'label':
				break;
			case 'euro':
			case 'num':
				$numX = (isset($aVar[$codV])) ? $aVar[$codV] : 0;
				$valX = number_format($numX,2,',','.');
				break;
		}
		$numY = 0;
		$valY = '';
		$diff = '';
		if ($aVarBP!=null) {
			switch ($aX['attr']['tipo_valore']) {
				case 'label':
					break;
				case 'euro':
				case 'num':
					$numY = (isset($aVarBP[$codV])) ? $aVarBP[$codV] : 0;
					$valY = number_format($numY,2,',','.');
					$diff = ($numY!=0) ? number_format(($numX-$numY)*100/abs($numY),1,',','.').'%' : '';
					break;
			}			
			
		} 
		echo "<tr><td class='txt1' colspan='12'>".str_repeat("&nbsp;",$liv*8)
				."<a href='calcDato.php?cd=$codX&bil1=".reqVar('id')."&bil2=".reqVar('id')
				."&xsn=".XSN."' class='txt1'>$codX</a> - ".$aX['attr']['descr']
				."</td><td class='txt1' align='right' colspan='3'>$valX</td>"
				."</td><td class='txt1' align='right' colspan='3'>$valY</td>"
				."</td><td class='txt1' align='right' colspan='2'>$diff</td><tr>\n";
	}
	foreach($aX['figli'] as $codY => $aY)  {
		echoDato($codY, $aY, $aVar, $aVarBP, $liv+1);
	}
}
